import React from 'react';
import { Link, History } from 'react-router';
import classNames from 'classnames';
import * as Storage from '../common/Storage';
import * as Key from '../config/Key';

class Header extends React.Component {

  constructor() {
    super();
  }

  static contextTypes = {
    history: React.PropTypes.history
  }

  static propTypes = {
      headerTitle : React.PropTypes.string,
      headerInfo : React.PropTypes.array,
      isGoBack : React.PropTypes.bool,
      headerType : React.PropTypes.string
  }

  static defaultProps = {
      headerTitle : "华西证券股票专家",
      isGoBack : false,
      headerInfo : [],
      headerType : "header-primary"
  }

  handleReturnBackClick(){
    let redirectPage = Storage.sessionStorageService.get(Key.redirectKey);
    let redirectFlag = false;
    if (redirectPage && redirectPage != "") {
      let currentHref = window.location.href.toLowerCase();
      redirectPage = redirectPage.toLowerCase();
      if (currentHref.indexOf(redirectPage) > 0) {
          redirectPage = true;
          Storage.sessionStorageService.remove(Key.redirectKey);
      }
    }

    let { isGoBack } = this.props;
    if (redirectPage == true) {
      let redirectPage = isGoBack || "Product";
      this.context.history.pushState(null, "/"+redirectPage);
    }else{
      window.history.go(-1);
    }
  }

  renderBackLink(){
    let { isGoBack } = this.props;
    return (
      <div className="header-return">
        {
            isGoBack ?
            <a onClick={ this.handleReturnBackClick.bind(this) } href="javascript:void(0);">
              <i className="iconfont icon-return"></i>
            </a>
            : null
        }
      </div>
    );
  }

  renderText(){

      let { headerTitle } = this.props;

      return (
        <div className="header-text">
           { headerTitle }
        </div>
      );
  }

  renderOperator(){
    return (
      <div className="header-op">
          {this.props.children}
      </div>
    );
  }

  render() {
    let { className, headerType, ...other } = this.props;
    let renderClass = classNames("header", className, headerType);

    return (
      <div className={renderClass} {...other}>
          {this.renderBackLink()}
          {this.renderText()}
          {this.renderOperator()}
      </div>
    );
  }
}

export default Header;
